package com.snk.kof.generator;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.snk.kof.mbp.base.AbstractDO;
import com.snk.kof.mbp.injector.RootMapper;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;

/**
 * @author keshan
 * @date 2022-01-09 下午7:14
 */
public class Main {

    private static final String author = "keshan";
    private static final String projectPath = System.getProperty("user.dir");
    private static final String projectModule = "/kof-application";
    private static final String javaPath = "/src/main/java";
    private static final String resourcePath = "/src/main/resources/mapper";
    private static final String parentPackage = "com.snk.kof.application";
    private static final String moduleName = "user";
    private static final String tableName = "sys_user";

    public static void main(String[] args) {

        FastAutoGenerator.create("jdbc:mysql://42.192.145.170:9999/kof97", "root", "zs94Power")
                .globalConfig(builder -> builder.author(author)
                        .dateType(DateType.TIME_PACK)
                        .disableOpenDir()
                        .commentDate(() -> LocalDateTimeUtil.format(LocalDateTime.now(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                        .outputDir(projectPath + projectModule + javaPath))
                .packageConfig(builder -> {
                    builder.parent(parentPackage)
                            .moduleName(moduleName)
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + projectModule + resourcePath));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableName)
                            .controllerBuilder()
                            .enableRestStyle()
                            .enableHyphenStyle()
                            .entityBuilder()
                            .enableLombok()
                            .enableChainModel()
                            .superClass(AbstractDO.class)
                            .addSuperEntityColumns("id", "is_delete", "create_time", "update_time")
                            .versionColumnName("version")
                            .versionPropertyName("version")
                            .mapperBuilder()
                            .superClass(RootMapper.class)
                            .enableMapperAnnotation()
                            .build();
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}
